//
//  Module_IOS.m
//  StarBugs
//
//  Created by 진권 정 on 6/22/12.
//  Copyright 2012 antisword@playorca.com. All rights reserved.
//
#include "Util/Module_ios.h"

#include "Scene/Scene_ChapterSelect.h"
#include "Scene/Scene_Game.h"
#include "Scene/Scene_MainMenu.h"
#include "Scene/Scene_Setting.h"
#include "Util/UserData.h"
#include "Global.h"

#ifdef __IOS_
#include "GameCenterTest.h"
#include "CrystalSlimlineSession.h"
#include "FlurryAnalytics.h"
#include "ShowAlert.h"
#include "InAppPurchase.h"
#endif


using namespace cocos2d;
Module_IOS *Module_IOS::pModule_IOS = NULL;

#ifdef __IOS_
NSString *parameterName[] =
{
    @"star1_clear",
    @"star2_clear",
    @"star3_clear",
    @"Stage Clear",
    @"Stage_Replay",
    @"Stage_Skip",
    @"Crystal",
    @"FaceBook",
    @"Gift",
    @"Setting",
};  

NSString *achievementIdString[] = 
{
    @"4704131849",
    @"4703976527",
    @"4704149763",
    @"4704173471",
    @"4704156577",
    
    @"4703973994",
    @"4704251362",
    @"4704191349",
    @"4728832776",
    @"4704205464",
    
    @"4704253214",
    @"4704256334",
    @"4704280190",
    @"4704261123",
    @"4704254255",
    
    @"4704300138",
    @"4704278225",
    @"4729306216",
};

NSString *achievementDescription[] = 
{
    @"Beginner",
    @"Expert",
    @"Master",
    @"Green Bean - perfect",
    @"Ice Box - perfect",
    
    @"Lollipop - perfect",
    @"Metal City - perfect",
    @"Dream of Ocean - perfect",
    @"Ghost Jungle - perfect", 
    @"UFO Driver",
    
    @"Teleporter",
    @"Lucky guy",
    @"Bubble Popper",
    @"Rookie Shooter",
    @"Skilled Shooter",
    
    @"Opener",
    @"Locker",
    @"Spider Man",
};

NSString *leaderboardIdString[] = 
{
    @"4704296460",
    @"4704263475",
    @"4729376733",
    @"4729212579",
    @"4728564750",
    @"4729529075",
    @"4779045247",
};
#endif

void Module_IOS::authenticateLocalUser()
{
#ifdef __IOS_
    [[GameCenterTest instance] authenticateLocalUser];
#endif
}
void Module_IOS::showAchievement()
{
#ifdef __IOS_
    [[GameCenterTest instance] showAchievement];
    hideCrystalSlimline();
#endif
}


void Module_IOS::showRank()
{
#ifdef __IOS_
    [[GameCenterTest instance] showLeaderboard];
    hideCrystalSlimline();
#endif
}

void Module_IOS::reportRank(double score)
{
#ifdef __IOS_
    [[GameCenterTest instance] reportScore:score];
#endif
    
}

void Module_IOS::showCrystalSlimline()
{
#ifdef __IOS_
    [CrystalSlimlineSession activateCrystalUIFromScreenEdge:@"bottom"];
#endif
}

void Module_IOS::hideCrystalSlimline()
{
#ifdef __IOS_
    [CrystalSlimlineSession deactivateCrystalUI];
#endif
}

void Module_IOS::postAchievement(int achievementId)
{
#ifdef __IOS_
    [[GameCenterTest instance] checkAchievement:achievementId];
#endif
}

void Module_IOS::openURL(const char *str)
{
#ifdef __IOS_
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithUTF8String:str]]];
#endif
}

void Module_IOS::flurry_event(int type, int chapter, int stage)
{
#ifdef __IOS_
    NSString *value;
    NSDictionary *dictionary;
    if(type < flurry_achievement)
    {
        value = [NSString stringWithFormat:@"%d_%d", chapter +1, stage + 1, NULL];
        dictionary = [NSDictionary dictionaryWithObjectsAndKeys:value, parameterName[type], nil];
        [FlurryAnalytics logEvent:@"Stage Event" withParameters:dictionary];
    }
    else
    {
        value = [NSString stringWithFormat:@"%@", parameterName[type], NULL];
        dictionary = [NSDictionary dictionaryWithObjectsAndKeys:value, @"mainmenu", nil];
        [FlurryAnalytics logEvent:@"Main Menu Event" withParameters:dictionary];
    }
#endif
}


void Module_IOS::reviewPopup()
{
#ifdef __IOS_
    ShowAlert *showAlert = [ShowAlert alloc];
    [showAlert openPopup];
#endif

}


void Module_IOS::callBack_restore()
{
#ifdef __IOS_
    ((Scene_Setting *)cocos2d::CCDirector::sharedDirector()->getRunningScene())->callback_restore();
#endif
}


void Module_IOS::callBack_closeGameKit()
{
#ifdef __IOS_
    showCrystalSlimline();
#endif
}

void Module_IOS::startStore()
{
#ifdef __IOS_
    [InAppPurchase initialize];
    [[InAppPurchase instance] startInApp];
#endif
}

void Module_IOS::buyStore(int type)
{
#ifdef __IOS_
    [[InAppPurchase instance] buyInApp:type];
#endif
}

void Module_IOS::restoreIAP()
{
#ifdef __IOS_
    [[InAppPurchase instance] restoreInApp];
#endif
}

bool Module_IOS::getIsIpad()
{
#ifdef __IOS_
    return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad);
#endif
    return false;
}

bool Module_IOS::getIs5()
{
#ifdef __IOS_
    return (cocos2d::CCDirector::sharedDirector()->getWinSize().height == 568);
#endif
#ifdef __ANDROID_
    return true;
#endif
//    return false;
}

float Module_IOS::getSceneH()
{
    if(getIs5())
        return 568;
    
    return 480;
}

float Module_IOS::getScaleFactor_w()
{
#ifdef __IOS_
    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
        return 768.f / 640.f;
    else
#endif
        return 1;
}

float Module_IOS::getScaleFactor_h()
{
#ifdef __IOS_
    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
        return 1024.f / 960.f;
    else
#endif
        return 1;
}

float Module_IOS::getFixedHeight(int height)
{
#ifdef __IOS_
    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
//        return 64 + (32 * (height - 480) / 120.f);
    
        return (64 / Module_IOS::getScaleFactor_w()) * height / 480.f;
    else
#endif
        return 0;
}

int Module_IOS::getImageScale()
{
#ifdef __IOS_
    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
        return 2;
    else
#endif
        return 1;
}

void Module_IOS::resetAchievement()
{
#ifdef __IOS_
    [[GameCenterTest instance] resetAchievement];
#endif
}

void Module_IOS::checkAchievmentMetadata()
{
#ifdef __IOS_
    [[GameCenterTest instance] checkAchievmentMetadata];
#endif
}



void Module_IOS::recieveInApp(int type)
{
    if(UserData::getInstencePtr()->currentScene == scenenum_game)
        ((Scene_Game *)cocos2d::CCDirector::sharedDirector()->getRunningScene())->callback_InApp(type);
    else if(UserData::getInstencePtr()->currentScene == scenenum_mainmenu)
        ((Scene_MainMenu *)cocos2d::CCDirector::sharedDirector()->getRunningScene())->callback_InApp(type);
    else if(UserData::getInstencePtr()->currentScene == scenenum_chaptersel)
        ((Scene_ChapterSelect *)cocos2d::CCDirector::sharedDirector()->getRunningScene())->callback_InApp(type);
}

